﻿if (typeof Axis == "undefined") Axis = {};
if (typeof Axis.Admin == "undefined") Axis.Admin = {};
var GridData = [];
var listLucky = [];
Axis.Admin.ListProject = {
    Init: function () {
        var thisObj = this;
        thisObj.Grid.Init();
        //  GetMemberList();
        thisObj.EventClick();
        Axis.Admin.ListProject.UpdateGiftLottery(999999, -1);

    },
    Grid: {
        ID: "#GridContents",
        PagesID: "#GridPages",
        Init: function () {
            var thisObj = this;
            var options = {
                //  url: 'Service/ProjectService.ashx?Action=GET',
                data: 'local',
                datatype: "json",
                colNames: [
                    'ID', 'First Name', 'Last Name', 'Email', 'Type', 'Type',

                     'CMND', '', 'ĐT Di Động', 'ĐT Cố Định', 'Ngày Sinh',

                    'Địa Chỉ', 'Quận', 'Tỉnh Thành', 'ProvinceID', 'Xác Nhận', ''//,'Xử lý'
                ],
                colModel: [
                    { name: 'RowNumber', index: 'ID', width: 50, hidden: true, frozen: true, hidedlg: true },
                    { name: 'FirstName', index: 'FirstName', width: 190, frozen: true },
                    { name: 'LastName', index: 'LastName', width: 60, frozen: true },
                    { name: 'Email', index: 'Email', width: 190, align: 'center', editable: true },
                    { name: 'TypeName', index: 'TypeName', width: 50, align: 'center', hidden: true, hidedlg: true },
                    { name: 'Type', index: 'Type', hidden: true, hidedlg: true },

                    { name: 'CMND', index: 'CMND', width: 70, align: 'center' },
                    { name: 'Email2', index: 'Email', width: 1, align: 'center', hidden: true },
                    { name: 'Mobile', index: 'Mobile', width: 70, align: 'center', hidden: true },
                    { name: 'Phone', index: 'Phone', width: 70, align: 'center', hidden: true },
                    {
                        name: 'DateOfBirth', index: 'DateOfBirth', width: 70, align: 'center', formatter: 'date', formatoptions: {
                            srcformat: "m/d/Y",
                            newformat: "d/m/Y"
                        }
                    },

                    { name: 'Address', index: 'Address', width: 170, hidden: true },
                    { name: 'Ward', index: 'Ward', width: 50, align: 'center', hidden: true },
                    { name: 'ProvinceName', index: 'ProvinceName', width: 90 },
                    { name: 'ProvinceID', index: 'ProvinceID', hidden: true, hidedlg: true },
                    { name: 'IsVerifiedString', index: 'Status', width: 60, align: 'center' },
                    { name: 'IsVerified', index: 'Status', width: 160, align: 'center', hidden: true }
                // ,{ name: 'Action', index: 'Action', width: 55, align: 'center' }
                ],
                afterInsertRow: function (rowid, aData, addData) {
                },

                onCellSelect: function (rowid, iCol, cellcontent, e) {

                },
                onSelectRow: function (rowid, status, e) {

                },
                ondblClickRow: function (rowid, iRow, iCol, e) {

                },

                height: 'auto',
                rownumbers: true,
                autowidth: true,
                //forceFit: true,
                shrinkToFit: false,
                rowNum: 20,
                pager: thisObj.PagesID
            };
            thisObj.GridObj = new CGrid(options);
            thisObj.GridObj.Init(thisObj.ID);
            thisObj.GridObj.NavGrid();
            thisObj.GridObj.FrozenColumns();
        },
        Reload: function () {
            var thisObj = this;
            if (thisObj.GridObj)
                thisObj.GridObj.ReloadGrid();
        },
        SelectRow: function () {
            var thisObj = this;
            if (thisObj.GridObj) {
                return thisObj.GridObj.SelectRow();
            }
            return null;
        }
    },
    GetMemberList: function () {
        var objThis = this;
        var projectID = $("#ddlProjectList").val();
        var pData = { ProjectID: projectID };
        $.ajax({
            url: 'Services/AxisLottery.asmx/GetListMember',
            type: 'POST',
            data: JSON.stringify(pData),
            cache: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (pData, textStatus, jqXHR) {
                var data = eval("(" + pData.d + ")");
                listNumber = data.length;
                $("#GridContents").clearGridData(true);
                GridData = data;
                $("#GridContents").addRowData("RowNumber", data);

                objThis.UpdateGiftLottery(999999, -1);
                $("body").removeClass('iMarkets-overlay-modal');
            },
            beforeSend: function (jqXHR, settings) {
                $("#GridContents").clearGridData(true);
                $("body").addClass('iMarkets-overlay-modal');
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //  overlay.Hide();
                $("body").removeClass('iMarkets-overlay-modal');
            },
            complete: function (XMLHttpRequest, textStatus) {
                // overlay.Hide();
            }
        });

    },
    GetGiftLotteryList: function () {
        var pData = { ProjectID: $("#ddlProjectList").val() };
        $.ajax({
            url: '../Accounts/Service/MemberService2.asmx/GetGiftLotteryListByID',
            type: 'POST',
            data: JSON.stringify(pData),
            cache: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (pData, textStatus, jqXHR) {
                var data = eval("(" + pData.d + ")");
                var htmlLottory = "";
                $.each(data, function (index, item) {
                    htmlLottory += $.string.Format("<option value='{0}'>{1}</option>", item.ID, item.Name);
                });
                $("#ddlGiftLottory").html(htmlLottory);
                $("body").removeClass('iMarkets-overlay-modal');
            },
            beforeSend: function (jqXHR, settings) {
                $("#ddlGiftLottory").html('');
                $("#ddlGift").html('');
                $("body").addClass('iMarkets-overlay-modal');
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //  overlay.Hide();
                $("body").removeClass('iMarkets-overlay-modal');
            },
            complete: function (XMLHttpRequest, textStatus) {
                // overlay.Hide();
            }
        });
    },
    UpdateGiftLottery: function (luckyNumber, timeWait) {
        var luckyGift = $("#ddlGiftLottory option:selected").text();
        //LuckyName, string LuckyNumber, string TimeWait, string LuckyGift, string ProjectName
        var projectName = $("#ddlProjectList option:selected").text();
        var index = parseInt(luckyNumber) - 1;
        var luckyName = "";
        if (index >= 0) {
            if (GridData.length >= index)
                luckyName = GridData[index].FirstName + " " + GridData[index].LastName;
        }
        var pData = { LuckyName: luckyName, LuckyGift: luckyGift, LuckyNumber: luckyNumber, TimeWait: timeWait, ProjectName: projectName, ProjectID: $("#ddlProjectList").val() };
        $.ajax({
            url: 'Services/AxisLottery.asmx/SetCurrentResult',
            type: 'POST',
            data: JSON.stringify(pData),
            cache: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (pData, textStatus, jqXHR) {
                var data = eval("(" + pData.d + ")");
                var htmlLottory = "";
                //$("body").removeClass('iMarkets-overlay-modal');
            },
            beforeSend: function (jqXHR, settings) {

                // $("body").addClass('iMarkets-overlay-modal');
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //  overlay.Hide();
                //$("body").removeClass('iMarkets-overlay-modal');
            },
            complete: function (XMLHttpRequest, textStatus) {
                // overlay.Hide();
            }
        });
    },
    EventClick: function () {
        var thisObj = this;
        $("#ddlProjectList").change(function () {
            thisObj.GetMemberList();
            thisObj.GetGiftLotteryList();

        });
    }
};
$(function () {
    Axis.Admin.ListProject.Init();
});
var listNumber = 0;

var timeRun = 0;
var loopTime = 0;
function StartLottory(obj) {
    LottoryClear();
    intervalLottery = setInterval(Ramdom, 1000);
    Axis.Admin.ListProject.UpdateGiftLottery(999999, -1);
    loopTime = timeRun - 1;
    window.setTimeout(function () {
        clearInterval(intervalLottery);
    }
    , (timeRun + 2) * 1000);

    return false;
}

var intervalLottery = null;
function Ramdom() {
    var x = 0;
    while (1) {
        x = Math.floor((Math.random() * listNumber) + 1);
        var exits = false;
        for (var i = 0; i < listLucky.length; i++)
            if (listLucky[i] == x)
                exits = true;
        if (exits == false) {
            listLucky.push(x);
            break;
        }
    }
    timeRun--;
    $("#ResultTotal").html(x);
    $("#divTime").html(timeRun);
    if (timeRun == 0) {
        Axis.Admin.ListProject.UpdateGiftLottery(x, 0);
        clearInterval(intervalLottery);
    }

}
function LottoryClear() {
    timeRun = parseInt($("#txtRun").val());
    loopTime = 1000;
    $("#ResultTotal").html(0);
    $("#divTime").html(0);
    clearInterval(intervalLottery);
};
